import { getEmitter } from "@/event";
import { UserLoginEvent, UserLogoutEvent } from '@/event/enums'

const emitter = getEmitter();

class UserAuth {
  private _isLogin: boolean
  private _token: string

  public get isLogin() {
    return this._isLogin;
  }

  public login(token:string) {
    this._token = token;
    if(token!=""){
        this._isLogin = true;
        emitter.emit(UserLoginEvent);
    }
    else if(this._isLogin) {
        this._isLogin = false;
        emitter.emit(UserLogoutEvent);
    }
  }

  public logout() {
    this._isLogin = false;
    emitter.emit(UserLogoutEvent);
  }

  /**清除令牌并登出 */
  public token() {
    this._token = "";
    return this._token;
  }
}


const auth = new UserAuth()

export {
  auth
}